package cn.entity.master;

import cn.entity.utils.Paint;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class master extends JPanel implements masterImp{
    private int index;
    private int x;
    private int y;
    public  int width;
    private int a;
    private int step;
    private int hitSpeed;
    private int bigTime;
    private BufferedImage[] images;
    private BufferedImage image;


    public master(int k,int x,int y,int kind,int hp) throws IOException {
        hitSpeed = 0;
        width=0;
        step=0;
        bigTime=0;
        a=0;
        this.x=x;
        this.y=y;
        if(kind==3){
            images = new BufferedImage[10];
            for(int i = 0; i < images.length; i++) {
                images[i]= ImageIO.read(new File("img/master/E"+i+".png"));
            }
            if(k==0){
                image=images[0];
            }
        }

    }
    @Override
    public int getX() {
        return x;
    }
    @Override
    public int getY() {
        return y;
    }
    public int getIndex(){
        return index;
    }
    public void setIndex(int index){
        this.index = index;
    }
    public int getImageLength(){
        return images.length;
    }
    public void setImage(int index){
        this.image = images[index];
    }
    public BufferedImage getImage(){
        return image;
    }
    public void master1Step() {
        if(width>450){
            if(y<=5){
                step=0;
            }
            if(y>=500){
                step=1;
            }
            switch (step){
                case 0:
                    y+=8;
                    break;
                case 1:
                    y-=8;
                    break;
            }
        }
        else if(width<450&& cn.entity.utils.Paint.a==1){
            if(cn.entity.utils.Paint.masterState==1){
                bigTime++;
              if(bigTime>100){
                 switch (a){
                     case 0:
                             y+=20;
                             x-=25;
                         if(y>=400) a=1;
                         break;
                     case 1:
                         y-=20;
                         if(y<=0){
                             a=2;
                         }
                         break;
                     case 2:
                         x+=20;
                         if(x>=500)
                         {
                             a=0;
                             cn.entity.utils.Paint.masterState=0;
                         }
                         break;
                 }
              }
            }
           else if(Paint.masterState==0){
               x=700;
               y=210;
                bigTime=0;
            }
        }
    }
    public boolean master1Come(){
        if(y<200){
            y+=5;
            return false;
        }
        return true;
    }
    public void paint(Graphics g){
        g.drawImage(getImage(),getX(),getY(),null);
        g.setColor(Color.RED);
        g.fillRect(15,530,width,10);
    }

    @Override
    public int getWidth() {
        return image.getWidth();
    }
   public int getHitWidth(){
        return width;
   }
    @Override
    public int getHeight() {
        return image.getHeight();
    }
    public void setY(int y){
        this.y=y;
    }
    public void setHp(int hit,int level){
        level-=1;
        if(hit==1){
            if(width>0){
                if(width>=450){
                    width-=width/(200-50*level);
                }
               else {
                   hitSpeed++;
                   if(hitSpeed==3){
                       width-=4+level;
                       hitSpeed=0;
                   }
                }
            }
        }
        if(hit==2){
            if(width>0){
                if(width>100){
                    width-=(width/100)*3+level;
                }
                else width-=3;

            }

        }
        if(hit==3){
            if(width>0){
                if(width>100){
                    width-=(width/100)+level;
                }
                else width-=2;
            }
        }
    }
    public void addWith(){hitSpeed++;
        if(width<750){
            width+=15;
        }
    }
}

